package cn.whpu.omnitrixerp.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import cn.idev.excel.context.AnalysisContext;
import cn.idev.excel.read.listener.ReadListener;
import cn.whpu.omnitrixerp.entity.dto.RegionDTO;
import cn.whpu.omnitrixerp.entity.po.basicdata.RegionPO;
import cn.whpu.omnitrixerp.mapper.basicdata.RegionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Gim Gong
 * @date 2025/5/31
 */
@Slf4j
@Component
public class RegionDataListener implements ReadListener<RegionDTO> {

    @Autowired
    private RegionMapper regionMapper;

    @Override
    public void invoke(RegionDTO regionDTO, AnalysisContext analysisContext) {
        try {

            regionDTO.setLevel(regionDTO.getLevel() + 1);
            log.info("正在处理：{}", JSONUtil.toJsonStr(regionDTO));
            RegionPO regionPO = new RegionPO();
            BeanUtil.copyProperties(regionDTO, regionPO);
            regionMapper.insert(regionPO);
            log.info("{}-{} 写入成功！", regionDTO.getCode(), regionDTO.getName());
        } catch (Exception e) {
            log.error("数据解析失败，错误信息：{}", e.getMessage());
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("数据解析完成");
    }


}
